package com.shangyueshang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.mapper.SysRoleMapper;
import com.shangyueshang.mapper.SysRoleMenuMapper;
import com.shangyueshang.model.SysMenu;
import com.shangyueshang.model.SysRole;
import com.shangyueshang.model.SysRoleMenu;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.SysMenuService;
import com.shangyueshang.service.SysRoleService;
import com.shangyueshang.utils.MyPageHelper;
import com.shangyueshang.utils.SecurityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/5 18:48
 * @version: v1.0.0
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
	
	@Resource
	private SysMenuService sysMenuService;
	
	@Resource
	private SysRoleMenuMapper sysRoleMenuMapper;
	
	@Override
	public HttpResult create(SysRole sysRole) {
		SysRole role = super.getById(sysRole.getId());
		if (role != null) {
			if (SysConstants.admin.equalsIgnoreCase(role.getName())) {
				return HttpResult.fail("超级管理员不允许修改");
			}
		} else {
			sysRole.setCreator(SecurityUtil.getUsername());
			sysRole.setCreateTime(LocalDateTime.now());
		}
		// 新增角色
		if ((sysRole.getId() == null || sysRole.getId() == 0)
				&& !this.findByName(sysRole.getName()).isEmpty()) {
			return HttpResult.fail("角色名已存在!");
		}
		sysRole.setLastUpdateTime(LocalDateTime.now());
		sysRole.setLastUpdator(SecurityUtil.getUsername());
		return HttpResult.ok(super.saveOrUpdate(sysRole));
	}
	
	@Override
	public PageResult findPage(PageReq pageReq) {
		QueryWrapper<SysRole> queryWrapper =
				new QueryWrapper<SysRole>().orderByDesc(SysConstants.create_time);
		pageReq.getLineParams().forEach(queryWrapper::eq);
		PageInfo<Object> pageInfo =
				PageHelper.startPage(pageReq.getPageNum(), pageReq.getPageSize())
						.doSelectPageInfo(() -> baseMapper.selectList(queryWrapper));
		return MyPageHelper.getPageResult(pageInfo);
	}
	
	@Override
	public List<SysMenu> findRoleMenus(Long roleId) {
		SysRole sysRole = super.getById(roleId);
		// 如果是超级管理员，返回全部
		if (sysRole != null && SysConstants.admin.equalsIgnoreCase(sysRole.getName())) {
			return sysMenuService.list();
		}
		return sysMenuService.findByRole(roleId);
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public HttpResult saveRoleMenus(List<SysRoleMenu> records) {
		for (SysRoleMenu roleMenu : records) {
			SysRole role = super.getById(roleMenu.getRoleId());
			if (role != null && SysConstants.admin.equalsIgnoreCase(role.getName())) {
				return HttpResult.fail("超级管理员拥有的所有菜单权限，不允许修改！");
			}
		}
		sysRoleMenuMapper.deleteByRole(records.get(0).getRoleId());
		records.forEach(it -> sysRoleMenuMapper.insert(it));
		return HttpResult.ok();
	}
	
	@Override
	public List<SysRole> findByName(String name) {
		QueryWrapper<SysRole> queryWrapper =
				new QueryWrapper<SysRole>().eq(SysConstants.name, name);
		return super.list(queryWrapper);
	}
}
